const router = require('koa-router')();
const serviceService = require('../service/serviceService');
const commonService = require('../service/commonService');
router.prefix('/api/service');

/**
 * @api {post} /api/service/addService 添加服务
 * @apiName addService
 * @apiGroup service
 *
 * @apiParam {String} name 服务名称.
 * @apiParam {String} number 序号.
 * @apiParam {string}  logo 图标.
 * @apiParam {String} introduce 简介.
 *
 * @apiSuccess {int} code 返回状态码（1：添加成功，2：添加失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/addService', async (ctx) => {
    try {
        let data = ctx.request.body;
        let name = data.name,
            number = data.number,
            logo = data.logo,
            introduce = data.introduce;
        if (!name || !number || !logo || !introduce) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await serviceService.handleAddService(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/service/editService 修改服务
 * @apiName editService
 * @apiGroup service
 *
 * @apiParam {String} id 服务id.
 * @apiParam {String} name 服务名称.
 * @apiParam {String} number 序号.
 * @apiParam {String} introduce 简介.
 * @apiParam {string}  logo 图标.
 *
 * @apiSuccess {int} code 返回状态码（1：修改成功，2：修改失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/editService', async (ctx) => {
    try {
        let data = ctx.request.body;
        let name = data.name,
            number = data.number,
            logo = data.logo,
            introduce = data.introduce,
            id = data.id;
        if (!name || !number || !logo || !introduce || !id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await serviceService.handleEditService(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/service/deleteService 删除服务
 * @apiName deleteService
 * @apiGroup service
 *
 * @apiParam {String} id 服务id.
 *
 * @apiSuccess {int} code 返回状态码（1：删除成功，2：删除失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/deleteService', async (ctx) => {
    try {
        let data = ctx.request.body;
        let id = data.id;
        if (!id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result, result1;
        result1 = await commonService.handleCheckRelate(data);
        if (result1.code == 1) {
            data.is_relate = 1;
        } else {
            data.is_relate = 0;
        }
        result = await serviceService.handleDeleteService(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/service/checkService 删除前判断当前服务是否被广告使用
 * @apiName checkService
 * @apiGroup service
 *
 * @apiParam {String} id 服务id.
 *
 * @apiSuccess {int} code 返回状态码（1：已被使用，2：未被使用，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/checkService', async (ctx) => {
    try {
        let data = ctx.request.body;
        let id = data.id;
        if (!id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await commonService.handleCheckRelate(data);
        if (result.code == 1) {
            result.msg = '已被使用';
        } else if (result.code == 2) {
            result.msg = '未被使用';
        }
        result.data = undefined;
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/service/getService 获取服务列表
 * @apiName getService
 * @apiGroup service
 *
 * @apiSuccess {int} code 返回状态码（1：获取成功，2：获取失败，3：服务器出错）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {json} data 获取成功时返回的数据
 *
 */

router.post('/getService', async (ctx) => {
    try {
        let result;
        result = await serviceService.handleGetService();
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/service/getServiceInfo 获取单个服务信息
 * @apiName getServiceInfo
 * @apiGroup service
 *
 * @apiParam {String} id 服务id.
 *
 * @apiSuccess {int} code 返回状态码（1：获取成功，2：获取失败，3：服务器出错）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {json} data 获取成功时返回的数据
 *
 */

router.post('/getServiceInfo', async (ctx) => {
    try {
        let data = ctx.request.body;
        let id = data.id;
        if (!id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await serviceService.handleGetServiceInfo(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

module.exports = router;